package com.nike.amoeba.adapter

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.Filter
import android.widget.Filterable
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatEditText
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.nike.amoeba.R
import com.nike.amoeba.bean.DataBean
import com.nike.amoeba.ext.setAdapterAnimation
import com.nike.amoeba.util.SettingUtil
import com.nike.amoeba.weight.MYListView
import org.json.JSONArray

/**
 * @Author nike
 * @Date 2023/7/7 17:05
 * @Description
 */
class StockAdjListAdapter (data: ArrayList<DataBean>) :
    BaseQuickAdapter<DataBean, BaseViewHolder>(
        R.layout.i_stock_adj_list, data), Filterable {

    init {
        setAdapterAnimation(SettingUtil.getListMode())
    }

    override fun convert(viewHolder: BaseViewHolder, bean: DataBean) {
        mFilterList[viewHolder.layoutPosition].run {
            viewHolder.setText(R.id.tv_title, context.getString(R.string.storage_location) + "：" + SUnitName)
            viewHolder.setText(R.id.tv_text1, context.getString(R.string.unit) + "：" + MUnitName)
            viewHolder.setText(R.id.tv_text2, context.getString(R.string.current_inventory) + "：" + Inventory)
            viewHolder.setText(R.id.tv_text3, context.getString(R.string.batch_number) + "：" + BatchNo)
            viewHolder.setText(R.id.tv_text4, context.getString(R.string.stock_adjustment) + "：")

            val etNumber = viewHolder.getView<AppCompatEditText>(R.id.et_number)
            viewHolder.getView<AppCompatButton>(R.id.bt_jia).setOnClickListener {
                try {
                    val toString = etNumber.text.toString()
                    var toInt = toString.toInt()
                    toInt +=1
                    etNumber.setText("$toInt")
                } catch (e: NumberFormatException) {

                }
            }

            viewHolder.getView<AppCompatButton>(R.id.bt_jian).setOnClickListener {
                try {
                    if (num <= 1){
                        return@setOnClickListener
                    }
                    num -= 1
                    etNumber.setText("$num")
                } catch (e: NumberFormatException) {

                }
            }
            val textWatcherNumber: TextWatcher = object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
                override fun afterTextChanged(s: Editable) {
                    if (s.isNotEmpty()) {
                        val toInt = s.toString().toInt()
                        num = toInt
                        if (num != toInt) {
                            etNumber.setText("$num")
                        }
                    }
                }
            }
            //入库数量
            if (etNumber.getTag() is TextWatcher) {
                etNumber.removeTextChangedListener(etNumber.getTag() as TextWatcher)
            }
            etNumber.setText("")
            etNumber.addTextChangedListener(textWatcherNumber)
            etNumber.setTag(textWatcherNumber)
        }
    }

    var mFilterList = ArrayList<DataBean>()

    fun appendList(list: List<DataBean>) {
        data = list as MutableList<DataBean>
        //这里需要初始化filterList
        mFilterList = list as ArrayList<DataBean>
    }

    override fun getFilter(): Filter {
        return object : Filter() {
            //执行过滤操作
            override fun performFiltering(charSequence: CharSequence): FilterResults {
                val charString = charSequence.toString()
                if (charString.isEmpty()) {
                    //没有过滤的内容，则使用源数据
                    mFilterList = data as ArrayList<DataBean>
                } else {
                    val filteredList: MutableList<DataBean> = ArrayList()
                    for (i in data.indices) {
                        val bean = data[i]
                        if (bean.AssetNo?.contains(charString, ignoreCase = true) == true
                        ) {
                            filteredList.add(bean)
                        }
                    }
                    mFilterList = filteredList as ArrayList<DataBean>
                }
                val filterResults = FilterResults()
                filterResults.values = mFilterList
                return filterResults
            }

            //把过滤后的值返回出来
            override fun publishResults(charSequence: CharSequence, filterResults: FilterResults) {
                mFilterList = filterResults.values as ArrayList<DataBean>
                notifyDataSetChanged()
            }
        }
    }


    override fun getItemCount(): Int {
        return mFilterList.size
    }

    override fun hashCode(): Int {
        return mFilterList.hashCode()
    }

    fun  getFilterList(): List<DataBean>{
        return mFilterList
    }

}